﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Controllers;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Services;
	using Falcon.Forums.Domain.Values;

	public class ForumPostController : DefaultController
	{
		public ForumPostController(
			IForumService forumService,
			IForumTopicService forumTopicService,
			IForumPostService forumPostService,
			IForumPostAttachmentService forumPostAttachmentService,
			IAttachmentStoreService attachmentStoreService)
		{
			this.ForumService = forumService;
			this.ForumTopicService = forumTopicService;
			this.ForumPostService = forumPostService;
			this.ForumPostAttachmentService = forumPostAttachmentService;
			this.AttachmentStoreService = attachmentStoreService;
		}

		public IForumService ForumService
		{
			get;
			private set;
		}

		public IForumTopicService ForumTopicService
		{
			get;
			private set;
		}

		public IForumPostService ForumPostService
		{
			get; 
			private set;
		}

		public IForumPostAttachmentService ForumPostAttachmentService
		{
			get;
			private set;
		}

		public IAttachmentStoreService AttachmentStoreService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Create(int id, bool quote)
		{
			var post = ForumPostService.GetById(id);

			if (post == null)
			{
				return Error(ForumsResources.Messages.ForumPostNotFound);
			}

			var forum = ForumService.GetById(post.ForumId);

			return forum.CanCreatePost ? View(Views.Create, new ForumPostCreate(forum, post, quote)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Create(ForumPostCreate value)
		{
			try
			{
				var forum = ForumService.GetById(value.ForumId);

				value.Forum = forum;

				if (!forum.CanCreatePost)
				{
					return NotAuthorized();
				}

				value.Attachment.Source = Request.Files[0];

				if (AttachmentStoreService.Process(value.Attachments, value.Attachment))
				{
					return View(Views.Create, value);
				}

				var post = ForumPostService.Create();

				ForumPostService.Insert(post, value);
				ForumTopicService.MarkUpdated(post);

				var topic = this.ForumTopicService.GetById(value.TopicId);

				return RedirectToRoute(ForumsRoutes.ForumTopicDetail, new { id = topic.Id, slug = topic.Slug });
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, value);
		}

		[AcceptVerbs(HttpVerbs.Get), Authorize]
		public ActionResult Update(int id)
		{
			var post = ForumPostService.GetById(id);

			if (post == null)
			{
				return Error(ForumsResources.Messages.ForumPostNotFound);
			}

			var forum = ForumService.GetById(post.ForumId, false);

			if (forum == null)
			{
				return Error(ForumsResources.Messages.ForumNotFound);
			}

			var value = new ForumPostUpdate(forum, post);

			value.Init();

			return forum.CanUpdateAnyPost || (forum.CanUpdatePost && post.CanUpdate) ? View(Views.Update, value) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post), Authorize]
		public ActionResult Update(ForumPostUpdate value)
		{
			try
			{
				value.Init();

				var post = ForumPostService.GetById(value.Id);

				if (post == null)
				{
					return Error(ForumsResources.Messages.ForumPostNotFound);
				}

				var forum = ForumService.GetById(post.ForumId, false);

				if (forum == null)
				{
					return Error(ForumsResources.Messages.ForumNotFound);
				}

				value.Forum = forum;

				if (!forum.CanUpdateAnyPost && (!forum.CanUpdatePost || !post.CanUpdate))
				{
					return NotAuthorized();
				}

				value.Attachment.Source = Request.Files[0];

				if (ForumPostAttachmentService.Process(post, value.Attachment, value.Attachments))
				{
					return View(Views.Update, value);
				}

				ForumPostService.Update(post, value);

				var topic = ForumTopicService.GetById(post.TopicId);

				return RedirectToRoute(ForumsRoutes.ForumTopicDetail, new { id = topic.Id, slug = topic.Slug });
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, value);
		}
	}
}